// pages/exam/index.js
const examApi = require('../../utils/examApi.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    examId: null,
    examInfo: {
      title: '',
      type: '',
      time: '',
      duration: 0,
      attempts: 1,
      illustration: '/images/exam-illustration.png'
    },
    // 新增分享和点赞相关数据
    shareCount: 132,
    likeCount: 386,
    loading: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('接收到的参数:', options);
    
    if (options.examId) {
      this.setData({
        examId: options.examId,
        loading: true
      });
      this.loadExamInfo(options.examId);
    } else {
      wx.showToast({
        title: '试卷ID不能为空',
        icon: 'error'
      });
      setTimeout(() => {
        wx.navigateBack();
      }, 1500);
    }
  },

  async loadExamInfo(examId) {
    try {
      const examPaper = await examApi.getExamPaperDetail(examId);
      console.log('获取到的试卷信息:', examPaper);
      
      // 使用试卷的实际开始和结束时间
      const startTime = examPaper.startTime ? new Date(examPaper.startTime) : new Date();
      const endTime = examPaper.endTime ? new Date(examPaper.endTime) : new Date(startTime.getTime() + examPaper.duration * 60000);
      const timeStr = `${this.formatDate(startTime)} ~ ${this.formatDate(endTime)}`;
      
      this.setData({
        examInfo: {
          title: examPaper.title,
          type: examPaper.type === 'MOCK' ? '模拟考试' : '专项练习',
          time: timeStr,
          duration: examPaper.duration,
          attempts: 1,
          illustration: '/images/exam-illustration.png'
        },
        loading: false
      });
    } catch (error) {
      console.error('加载试卷信息失败:', error);
      wx.showToast({
        title: error.message || '加载失败',
        icon: 'none'
      });
      this.setData({ loading: false });
    }
  },

  formatDate(date) {
    if (!date || isNaN(date.getTime())) {
      return '待定';
    }
    
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hour = String(date.getHours()).padStart(2, '0');
    const minute = String(date.getMinutes()).padStart(2, '0');
    return `${year}.${month}.${day} ${hour}:${minute}`;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: this.data.examInfo.title,
      path: `/pages/exam/index?examId=${this.data.examId}`
    };
  },

  /**
   * 用户点击分享按钮
   */
  shareExam() {
    wx.showToast({
      title: '分享功能已启用',
      icon: 'success'
    });
    // 实际项目中可以在这里增加分享次数的统计
    this.setData({
      shareCount: this.data.shareCount + 1
    });
  },

  // 用户点赞
  likeExam() {
    wx.showToast({
      title: '感谢点赞！',
      icon: 'success'
    });
    this.setData({
      likeCount: this.data.likeCount + 1
    });
  },

  startExam() {
    console.log('开始考试，跳转到答题页面');
    
    if (!this.data.examId) {
      wx.showToast({
        title: '试卷ID不能为空',
        icon: 'error'
      });
      return;
    }
    
    wx.navigateTo({
      url: `/pages/question/index?examId=${this.data.examId}`,
      success: function() {
        console.log('跳转成功');
      },
      fail: function(error) {
        console.error('跳转失败:', error);
        wx.showToast({
          title: '跳转失败，请重试',
          icon: 'none'
        });
      }
    });
  }
})